package com.wanmao.test.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;
import android.util.Log;
import android.widget.CheckBox;

import com.wanmao.test.bean.CheckBoxBean;
import com.wanmao.test.shebei.Item;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class PerferenceUntil {
    private static SharedPreferences.Editor editor;
    private static final String SB_ID = "SB_ID";// 设备ID
    private static final String ISFIST = "IS_FIRST";// 是否是第一次
    private static final String QUESTION_DESCRIBE= "describe";// 问题描述
    private static final String RESULT= "result";// 问题描述
    private static final String TIME= "time";// 设置默认时间

    private static final String IS_FIRST_IN = "IS_FIRST_IN";// 是否是第一次进入app:false:第一次,true:不是第一次


    public static void shareProject(String kye,Context contenxt, String ID, boolean isfist) {
        SharedPreferences sp = contenxt.getSharedPreferences(kye, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putString(SB_ID, ID);
        editor.putBoolean(ISFIST, isfist);
        editor.commit();
    }
    public static void shareLogin(Context contenxt, boolean isfist) {
        SharedPreferences sp = contenxt.getSharedPreferences("login", Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putBoolean(ISFIST, isfist);
        editor.commit();
    }

    public static void shareTask(String kye,Context contenxt, String describe,String result,String JDYD_ID) {
        SharedPreferences sp = contenxt.getSharedPreferences(kye, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putString(QUESTION_DESCRIBE, describe);
        editor.putString("JDYD_ID", JDYD_ID);
        editor.putString(RESULT, result);
        editor.commit();
    }
    public static void shareTime(Context contenxt, String time) {
        SharedPreferences sp = contenxt.getSharedPreferences("time", Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putString(TIME, time);

        editor.commit();
    }
    public static void shareIstrue(String key,Context contenxt, int istrue,String id,boolean isfast) {
        SharedPreferences sp = contenxt.getSharedPreferences(key, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putInt("istrue",istrue);
        editor.putString("id",id);
        editor.putBoolean("isboon",isfast);
        editor.commit();
    }
    public static void shareXZ(String key,Context contenxt, int istrue,String xm_id,boolean isfast,boolean isfist) {
        SharedPreferences sp = contenxt.getSharedPreferences(key, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putInt("xize_size",istrue);
        editor.putString("XM_ID", xm_id);
        editor.putBoolean("isOne",isfast);
        editor.putBoolean("isfist",isfist);
        editor.commit();
    }
    public static void shareend(String XZID,Context contenxt, int number) {
        SharedPreferences sp = contenxt.getSharedPreferences(XZID, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.putInt("end_number",number);
        editor.commit();
    }

    /**
     * 读取细则详情页设备选择情况
     */
    public static List<CheckBoxBean> getCheckBox(Context mContext,String RWID) {
        SharedPreferences mSharedPreference1 = mContext.getSharedPreferences(RWID+"_CheckBoxBeanList", mContext.MODE_PRIVATE);
        List<CheckBoxBean> list=new ArrayList<>();
        int size = mSharedPreference1.getInt("CheckBoxBeanList_size", 0);
        if (size == 0) {
            return null;
        };
        for (int i = 0; i < size; i++) {
            String itemString = mSharedPreference1.getString("CheckBoxBeanList_" + i, null);

            if (!(itemString.equals("") && itemString == null)) {
                byte[] base64SItem = Base64.decode(itemString, Base64.DEFAULT);
                ByteArrayInputStream bais = new ByteArrayInputStream(base64SItem);
                try {
                    ObjectInputStream ois = new ObjectInputStream(bais);
                    CheckBoxBean item = (CheckBoxBean) ois.readObject();
                    list.add(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        return list;
    }

    public static boolean saveCheckBox(Context mContext, List<CheckBoxBean> list,String RWID) {
        SharedPreferences sp = mContext.getSharedPreferences(RWID+"_CheckBoxBeanList", mContext.MODE_PRIVATE);
        SharedPreferences.Editor mEditSave = sp.edit();
        mEditSave.putInt("CheckBoxBeanList_size", list.size());
        Log.e("CheckBoxBeanList", "size:" + list.size());
        for (int i = 0; i < list.size(); i++) {
            mEditSave.remove("CheckBoxBeanList_" + i);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(3000);
            ObjectOutputStream oos = null;
            try {
                oos = new ObjectOutputStream(baos);
                oos.writeObject(list.get(i));
            } catch (IOException e) {

                e.printStackTrace();
            }

            String base64SItem = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);

            mEditSave.putString("CheckBoxBeanList_" + i, base64SItem);
        }
        return mEditSave.commit();
    }



}
